<?php
$this->load->view('admin/layout/head', array('title'=>'用户管理', 'nav' => 'user'), FALSE);
?>
<link href="https://cdn.bootcss.com/bootstrap-datetimepicker/3.1.4/css/bootstrap-datetimepicker.min.css" rel="stylesheet">

<div id="container">
	<form class="form-inline" id="user-search-form">

	  <div class="form-group">
	    <input type="text" name="keyword" class="form-control" style="width:300px" placeholder="姓名/手机号/账号">
	  </div>
	  <a href="javascript:;" class="btn btn-info user-search-btn"><?= $this->lang->line('search') ?></a>
	  <a href="javascript:;" class="btn btn-default user-add-btn"><span class="glyphicon glyphicon-plus"></span> <?= $this->lang->line('create') ?></a>
	  <a href="/admin/user/multi_add" class="btn btn-default"><span class="glyphicon glyphicon-th"></span> 批量添加</a>
	</form>
	<div class="table-container mt-10">
		<table class="table table-bordered table-striped table-hover scroll-table">
			<thead >
				<tr>
					<th width="60" class="text-center">编号</th>
					<th width="60" class="text-center">昵称</th>
					<th width="60" class="text-center">姓名</th>
					<th width="100" class="text-center">手机号</th>
					<th width="60" class="text-center">余额</th>
					<th width="100" class="text-center">最后登录</th>
					<th width="100" class="text-center">添加时间</th>
					<th width="60" class="text-center">锁定</th>
					<th width="80" class="text-center">操作</th>
				</tr>
			</thead>
			<tbody class="text-center">
				<?php
				foreach ($users as $u) {
				?>
				<tr>
					<td><?php echo $u['id'] ?></td>
					<td><?php echo $u['nickname'] ?></td>
					<td><?php echo $u['username'] ?></td>
					<td><?php echo $u['phone'] ?></td>
					<td><?php echo $u['balance'] ?></td>
					<td><?php echo $u['login_at']?$u['login_at']:'未登录' ?></td>
					<td><?php echo $u['create_at'] ?></td>
					<td is_lock="<?php echo $u['status'] ?>">
						<?php echo $u['status']?'<span class="text-danger">禁止登录</span>':'<span class="text-success">允许登录</span>' ?>
					</td>
					<td>
						<a href="javascript:;" class="recharge-btn" user_id="<?= $u['id'] ?>">充值</a>
						<a href="javascript:;" class="modify-btn" user_id="<?php echo $u['id'] ?>">查看</a> |
						<a href="javascript:;" class="del-btn" user_id="<?php echo $u['id'] ?>">删除</a>
					</td>
				</tr>
				<?php
				}
				?>

			</tbody>
			<?php
			if($users->last_page>1){
			?>
			<tfoot>
				<td colspan="9" style="text-align:right">
					<?php echo $users->render() ?>
				</td>
			</tfoot>
			<?php
			}
			?>

		</table>
	</div>
</div>

<div id="form-div" style="display: none">
	<form id="user-form" class="form-horizontal">
	  <input type="hidden" name="id" value="0">
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">昵称：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="nickname" value="" class="form-control" placeholder="" tautocomplete="off">
	    </div>
	  </div>
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">微信：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="wechat" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">真实姓名：</label>
	    <div class="col-sm-3">
	    	<input type="text" name="first_name" value="" class="form-control" placeholder="firstname">
	    </div>
	    <div class="col-sm-3">
	    	<input type="text" name="last_name" value="" class="form-control" placeholder="lastname">
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">手机号：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="phone" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">密码：</label>
	    <div class="col-sm-6">
	    	<input type="password" name="password" value="" class="form-control" placeholder="密码">
	    </div>
	  </div>
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">支付密码：</label>
	    <div class="col-sm-6">
	    	<input type="password" name="pay_pass" value="" class="form-control" placeholder="支付密码">
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">邮箱：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="email" value="" class="form-control" placeholder="">
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">生日：</label>
	    <div class="col-sm-6">
	    	<input type="text" id="datetimepicker" name="birthday" value="" class="form-control" placeholder="">
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">性别：</label>
	    <div class="col-sm-6">
	    	<select name="sex" id="" class="form-control">
	    		<option value="0">男</option>
	    		<option value="1">女</option>
	    	</select>
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">用户等级：</label>
	    <div class="col-sm-6">
	    	<select name="level" class="form-control">
				<?php foreach ($user_levels as $key => $value): ?>
					<option value="<?= $value['id'] ?>"><?= "{$value['name']} ({$value['discount']})" ?></option>
				<?php endforeach; ?>
	    	</select>
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">备注：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="remark" value="" class="form-control" placeholder="">
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">允许登录：</label>
	    <div class="col-sm-6">
	    	<input type="radio" name="status" value="0" checked=""> 允许
	    	<input type="radio" name="status" value="1"> 不允许
	    </div>
	  </div>
	</form>
</div>

<div id="recharge-div" style="display: none">
	<form id="recharge-form" class="form-horizontal">
		<div class="form-group row">
		    <label class="col-sm-3 control-label">充值金额：</label>
		    <div class="col-sm-6">
		    	<input type="number" name="fee" value="" class="form-control" placeholder="输入充值金额">
		    </div>
		</div>
		<div class="form-group row">

		</div>
	</form>
</div>

<?php
$this->load->view('admin/layout/footer');
?>
<script src="https://cdn.bootcss.com/moment.js/2.18.1/moment-with-locales.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap-datetimepicker/3.1.4/js/bootstrap-datetimepicker.min.js"></script>

<script type="text/javascript">
	$(function(){
		var form_html = $('#form-div').html();
		var recharge_html = $('#recharge-div').html();
		$('#form-div').remove();
		$('#recharge-div').remove();

		//搜索
		$('.user-search-btn').on('click',function(){
			window.location = '/admin/user/?keyword='+$('input[name=keyword]').val();
		})

		//添加
		$('.user-add-btn').on('click',function(){
			var btn = '<a href="javascript:;" class="btn btn-primary user-form-submit">提交</a>';
			var dlg = dialog('<?= $this->lang->line('create') ?>',form_html,btn);

			$('.user-form-submit').on('click',form_submit);
			var lang = '<?= $this->session->admin_language == 'chinese' ? 'zh-CN' : 'en-au' ?>';
			// 日期控件绑定
			$('#datetimepicker').datetimepicker({
				format: 'YYYY-MM-DD',
				autoclose: true,
				startView: 1,
				minView: 2,
				language: lang
			});
		});

		//修改
		$('.modify-btn').on('click',function(){
			var tr = $(this).parents('tr');
			var id = tr.find('td').eq(0).text();
			var btn = '<a href="javascript:;" class="btn btn-primary user-form-submit">提交</a>';
			var dlg = dialog('<?= $this->lang->line('edit') ?>',form_html,btn);
			$.getJSON('/admin/user/detail/'+id, function (json) {
				for(var k in json) {
					$('#user-form input[name='+k+']').val(json[k]);
					if ('is_lock' == k) {
						$('#user-form input[name=status]').eq(json[k]).prop('checked',true);
					}
					if ('level' === k) {
						var options = $('#user-form select[name=level]').children();
						for(var index in options) {
							if (options[index].value == json[k]) {
								options[index].selected = true;
							}
						}
					}
				}
			});

			$('.user-form-submit').on('click',form_submit);

			var lang = '<?= $this->session->admin_language == 'chinese' ? 'zh-CN' : 'en-au' ?>';
			// 日期控件绑定
			$('#datetimepicker').datetimepicker({
				format: 'YYYY-MM-DD',
				autoclose: true,
				startView: 1,
				minView: 2,
				language: lang
			});
		})

		//删除
		$('.del-btn').on('click',function(){
			var user_id = $(this).attr('user_id');
			confirm('确定要删除此用户吗？',function(e){
				if(e.index == 1){
					$.post('/admin/user/delete', {id: user_id}, function(data, textStatus, xhr) {
						alert('删除成功！');
						window.location.reload();
					});
				}
			})

		})

		$('.recharge-btn').on('click', function () {
			var user_id = $(this).attr('user_id');
			var btn = '<a href="javascript:;" class="btn btn-primary r-submit">提交</a>';
			var dlg = dialog('<?= $this->lang->line('recharge') ?>',recharge_html, btn);
			$('.r-submit').on('click', function () {
				var formdata = $(this).parents('.modal').find('form').serialize();
				$.ajax({
					url: '/admin/user/recharge/'+user_id,
					type: 'post',
					data: formdata,
					success: function () {
						alert('充值成功');
						window.location.reload();
					}
				})
			});
		});
	})

	function form_submit(){
		$.post('/admin/user/deal', $('#user-form').serialize(), function(json, textStatus, xhr) {
			if(json.code == 1){
				alert(json.msg);
				return;
			}
			Toast.success('操作成功',function(){
				window.location.reload();
			})

		});
	}
</script>
